<?php


namespace app\home\controller;


use app\base\BaseController;
use think\facade\Db;
use think\facade\View;

class BuyDetail extends BaseController
{
    protected $title = '采购明细';
    protected $table = 'sale_contract_detail';
    protected $sType=2;
    public function index()
    {
        $param = $this->request->param();
        if (request()->isAjax()) {
            $where[] = ['scd.status', '=', 4];
            $where[] = ['scd.type', '=', $this->sType];

            if (isset($this->param['status']) && $this->param['status']) {
                $where[] = ['qo.status', '=', $this->param['status']];
            }
            if (isset($this->param['pName']) && $this->param['pName']) {
                $where[] = ['py.name', 'like', '%' . $this->param['pName'] . '%'];
            }
            if (isset($this->param['buyUnit']) && $this->param['buyUnit']) {
                $where[] = ['d.title', 'like', '%' . $this->param['buyUnit'] . '%'];
            }
            if (isset($this->param['buyName']) && $this->param['buyName']) {
                $where[] = ['ad.name', 'like', '%' . $this->param['buyName'] . '%'];
            }
            if (isset($this->param['orderNum']) && $this->param['orderNum']) {
                $where[] = ['qo.orderNum', 'like', '%' . $this->param['orderNum'] . '%'];
            }
            if (isset($this->param['brand']) && $this->param['brand']) {
                $where[] = ['md.brand', 'like', '%' . $this->param['brand'] . '%'];
            }
            if (isset($this->param['specs']) && $this->param['specs']) {
                $where[] = ['md.specs', 'like', '%' . $this->param['specs'] . '%'];
            }
            if (isset($this->param['buyCode']) && $this->param['buyCode']) {
                $where[] = ['md.buyCode', 'like', '%' . $this->param['buyCode'] . '%'];
            }
            if (isset($this->param['orderDate']) && $this->param['orderDate']) {
                $dates = explode('~', $this->param['orderDate']);
                $where[] = ['qo.orderDate', 'between', $dates];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('scd')
                ->leftJoin('sale_contract sc', 'sc.id=scd.orderId')
                ->leftJoin('purveyor py', 'py.id=sc.punterId')
                ->leftJoin('department d', 'd.id=sc.salesUnitId')
                ->leftJoin('admin ad', 'ad.id=sc.saleId')
                ->leftJoin('material_dossier md', 'md.id=scd.mdId')
                ->where($where)
                ->field('scd.*,py.name as pName,d.title as dName,ad.name as saleName,md.bar_code,md.name as mdName
                ,md.brand,md.specs,md.buyCode,md.unit,sc.orderDate,sc.orderNum')
                ->order('scd.id DESC')
                ->paginate($rows)->each(function ($item) {

                    $item['inNum'] = Db::name('stock_order_detail')
                        ->where('status', '=', 2)
                        ->where('saleContractDetailId', $item['id'])
                        ->where('mdId', $item['mdId'])
                        ->where('type', '=',3)
                        ->sum('num');
                    $item['refundNum'] = Db::name('stock_order_detail')
                        ->where('status', '=', 2)
                        ->where('saleContractDetailId', $item['id'])
                        ->where('mdId', $item['mdId'])
                        ->where('type', '=', 4)
                        ->sum('num');
                    $ztNum=$item['bjNum']-$item['inNum'];
                    $item['ztNum']=$ztNum<0?0:$ztNum;
                    return $item;
                });
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }

}